package com.gamesnet.ihm;

import java.util.List;

import org.json.JSONException;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;

import com.gamesnet.api.Api;
import com.gamesnet.api.Session;
import com.gamesnet.models.Listable;
import com.gamesnet.models.Publication;
import com.gamesnet.models.Utilisateur;
import com.gamesnet.util.AdapterListener;
import com.gamesnet.util.MessageDialog;
import com.gamesnet.widget.ActionBar;
import com.gamesnet.widget.ActionBar.IntentAction;
import com.gamesnet.widget.ListAdapter;

public class WallActivity extends Activity implements AdapterListener{
	
	Utilisateur user = null;
	long userId;
	@Override
	public void onCreate(Bundle savedInstanceState) { 

		super.onCreate(savedInstanceState); 
		setContentView(R.layout.wall);

		final ActionBar actionBar = (ActionBar) findViewById(R.id.actionbarHaut);
		actionBar.setHomeAction(new IntentAction(this, new Intent(this, DashboardActivity.class), R.drawable.home));
		actionBar.setTitre(PageTitle.WALL);
		
		Bundle b = getIntent().getExtras();
		Log.v("compteactivity",getIntent().toString());
		userId = b.getLong("userId");
		
		if(Session.getInstance().getUser().getId() == userId){
			 user = Session.getInstance().getUser();
			 actionBar.setTitre(PageTitle.WALL);
		}else{
			try {
				user = Api.getUser(String.valueOf(userId));
				actionBar.setTitre(PageTitle.WALL2+" "+user.afficher());
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}


		try {
			List<Publication>publications = Api.wall(String.valueOf(user.getId()), "0");
			ListAdapter<Publication> adapter = new ListAdapter<Publication>(this, publications);
			adapter.addListener(this);
			adapter.setUserId(userId);
			ListView list = (ListView)findViewById(R.id.listPublication);
			list.setAdapter(adapter);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}



	}

	public void onClickText(Listable model, int position) {
		Intent intent = new Intent(this, PublicationActivity.class);
		intent.putExtra("postId", model.getId());
		startActivity(intent);

	}

	public void onClickSupp(Listable model, int position) {
		MessageDialog.showMessage("supprimer"+model.afficher(), this);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) { 
		super.onCreateOptionsMenu(menu);
		menu.add(0, 1, 0, "Publiez");
		return true; 
	}

	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		switch (item.getItemId()) { case 1:
			Intent intent = new Intent(this, NewPublicationActivity.class);
			intent.putExtra("sender", userId);
			startActivity(intent);
			return true;
		default:
			return true;
		} 
	}

}
